<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Section 5.2.5: Mixed strategies for matrix games</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch05_duality/html/matrix_games.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Section 5.2.5: Mixed strategies for matrix games</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Jo&euml;lle Skaf - 08/24/05</span>
<span class="comment">%</span>
<span class="comment">% Player 1 wishes to choose u to minimize his expected payoff u'Pv, while</span>
<span class="comment">% player 2 wishes to choose v to maximize u'Pv, where P is the payoff</span>
<span class="comment">% matrix, u and v are the probability distributions of the choices of each</span>
<span class="comment">% player (i.e. u&gt;=0, v&gt;=0, sum(u_i)=1, sum(v_i)=1)</span>

<span class="comment">% Input data</span>
randn(<span class="string">'state'</span>,0);
n = 10;
m = 10;
P = randn(n,m);

<span class="comment">% Optimal strategy for Player 1</span>
fprintf(1,<span class="string">'Computing the optimal strategy for player 1 ... '</span>);

cvx_begin
    variable <span class="string">u(n)</span>
    minimize ( max ( P'*u) )
    u &gt;= 0;
    ones(1,n)*u == 1;
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);
obj1 = cvx_optval;

<span class="comment">% Optimal strategy for Player 2</span>
fprintf(1,<span class="string">'Computing the optimal strategy for player 2 ... '</span>);

cvx_begin
    variable <span class="string">v(m)</span>
    maximize ( min (P*v) )
    v &gt;= 0;
    ones(1,m)*v == 1;
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);
obj2 = cvx_optval;

<span class="comment">% Displaying results</span>
disp(<span class="string">'------------------------------------------------------------------------'</span>);
disp(<span class="string">'The optimal strategies for players 1 and 2 are respectively: '</span>);
disp([u v]);
disp(<span class="string">'The expected payoffs for player 1 and player 2 respectively are: '</span>);
[obj1 obj2]
disp(<span class="string">'They are equal as expected!'</span>);
</pre>
<a id="output"></a>
<pre class="codeoutput">
Computing the optimal strategy for player 1 ...  
Calling sedumi: 21 variables, 11 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
Split 1 free variables
eqs m = 11, order n = 23, dim = 23, blocks = 1
nnz(A) = 140 + 0, nnz(ADA) = 121, nnz(L) = 66
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            7.35E+00 0.000
  1 :  -3.40E-01 3.21E+00 0.000 0.4368 0.9000 0.9000   2.45  1  1  5.7E+00
  2 :  -3.22E-02 1.48E+00 0.000 0.4622 0.9000 0.9000   4.31  1  1  1.2E+00
  3 :  -2.23E-02 4.55E-01 0.000 0.3066 0.9000 0.9000   1.55  1  1  3.2E-01
  4 :  -2.93E-02 1.22E-01 0.000 0.2688 0.9000 0.9000   1.09  1  1  7.8E-02
  5 :  -2.82E-02 2.40E-02 0.000 0.1962 0.9000 0.9000   1.06  1  1  1.6E-02
  6 :  -2.79E-02 4.59E-03 0.000 0.1912 0.9000 0.9000   1.02  1  1  3.0E-03
  7 :  -2.79E-02 3.05E-05 0.000 0.0066 0.9990 0.9820   1.00  1  1  
iter seconds digits       c*x               b*y
  7      0.0  14.9 -2.7855878954e-02 -2.7855878954e-02
|Ax-b| =   2.4e-16, [Ay-c]_+ =   1.4E-16, |x|=  1.1e+00, |y|=  4.4e-01

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    3.000E-02    1.000E-02    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 2.62609.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.0278559
Done! 
Computing the optimal strategy for player 2 ...  
Calling sedumi: 21 variables, 11 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
Split 1 free variables
eqs m = 11, order n = 23, dim = 23, blocks = 1
nnz(A) = 140 + 0, nnz(ADA) = 121, nnz(L) = 66
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            7.35E+00 0.000
  1 :  -3.13E-01 3.18E+00 0.000 0.4325 0.9000 0.9000   2.46  1  1  5.6E+00
  2 :  -1.67E-02 1.45E+00 0.000 0.4560 0.9000 0.9000   4.28  1  1  1.2E+00
  3 :   1.13E-02 4.36E-01 0.000 0.3006 0.9000 0.9000   1.54  1  1  3.0E-01
  4 :   2.61E-02 1.16E-01 0.000 0.2652 0.9000 0.9000   1.09  1  1  7.3E-02
  5 :   2.74E-02 2.30E-02 0.000 0.1992 0.9000 0.9000   1.06  1  1  1.5E-02
  6 :   2.77E-02 4.39E-03 0.000 0.1906 0.9000 0.9000   1.01  1  1  2.9E-03
  7 :   2.79E-02 1.24E-05 0.000 0.0028 0.9990 0.9990   1.00  1  1  
iter seconds digits       c*x               b*y
  7      0.0  15.0  2.7855878954e-02  2.7855878954e-02
|Ax-b| =   1.7e-16, [Ay-c]_+ =   1.7E-17, |x|=  9.0e-01, |y|=  5.4e-01

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    3.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.49432.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.0278559
Done! 
------------------------------------------------------------------------
The optimal strategies for players 1 and 2 are respectively: 
    0.1804   -0.0000
    0.0000    0.3254
    0.0000    0.0924
    0.1549    0.0000
    0.1129         0
    0.0000    0.0264
    0.0000    0.4099
    0.1003    0.0509
    0.1474    0.0949
    0.3040   -0.0000

The expected payoffs for player 1 and player 2 respectively are: 

ans =

    0.0279    0.0279

They are equal as expected!
</pre>
</div>
</body>
</html>